package javaseof10day.day9.am.demo2;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;



import java.sql.*;

public class StudentManagement extends JFrame implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	JTabbedPane dbTabPane;
	JPanel inputPanel; // 录入面板
	JPanel viewPanel; // 浏览面板
	JPanel updatePanel; // 更新面板
	JPanel deletePanel; // 删除面板

	JButton inputBtn; // 录入
	JButton clearBtn1;
	StudentPanel inputInnerPanel;

	JTextArea viewArea; // 浏览
	JButton viewBtn;

	StudentPanel updateInnerPanel; // 修改数据
	JLabel updateInputLbl;
	JTextField updateInputText;
	JButton updateBtn;

	StudentPanel deleteInnerPanel;
	JLabel inputNoLabel;
	JTextField inputNoField;
	JButton deleteBtn;

	public StudentManagement() {
		super("学生基本信息管理系统");
		serGUIComponent();
	}

	public void serGUIComponent() {
		// TODO Auto-generated method stub
		Container c = getContentPane();
		c.setLayout(new BorderLayout());
		dbTabPane = new JTabbedPane();

		// 定义录入面板
		inputPanel = new JPanel();
		inputPanel.setLayout(new FlowLayout());
		inputBtn = new JButton("录入");
		clearBtn1 = new JButton("清除");
		/* 使用this对象，可以不用声明内部类而直接在本类中实现接口覆盖接口的方法。 */
		inputBtn.addActionListener(this);
		clearBtn1.addActionListener(this);
		inputInnerPanel = new StudentPanel();
		inputPanel.add(inputInnerPanel);
		inputPanel.add(inputBtn);
		inputPanel.add(clearBtn1);
		dbTabPane.add("录入数据", inputPanel);

		// 定义浏览面板
		viewPanel = new JPanel();
		viewPanel.setLayout(new BorderLayout());
		viewArea = new JTextArea(6, 35);
		viewBtn = new JButton("浏览");
		viewPanel.add(new JScrollPane(viewArea), BorderLayout.CENTER);
		viewPanel.add(viewBtn, BorderLayout.SOUTH);
		viewBtn.addActionListener(this);
		dbTabPane.addTab("浏览数据", viewPanel);

		// 定义更新面板
		updateInnerPanel = new StudentPanel();
		updateInputLbl = new JLabel("输入学号：");
		updateInputText = new JTextField(10);
		updateInputText.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 将更新面板的显示内容清除
				try {
					viewARecord(updateInputText.getText(), updateInnerPanel);
				} catch (ClassNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				updateInputText.setText("");
			}
		});
		updateBtn = new JButton("修改");
		updateBtn.addActionListener(this);
		updatePanel = new JPanel();
		updatePanel.add(updateInnerPanel);
		updatePanel.add(updateInputLbl);
		updatePanel.add(updateInputText);
		updatePanel.add(updateBtn);
		dbTabPane.add("修改数据", updatePanel);

		// 定义删除面板
		deleteInnerPanel = new StudentPanel();
		inputNoLabel = new JLabel("输入学号：");
		inputNoField = new JTextField(10);
		inputNoField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 将删除面板的显示内容清除
				try {
					viewARecord(inputNoField.getText(), deleteInnerPanel);
					
					inputNoField.setText("");
				} catch (ClassNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
		
			}
		});
		deleteBtn = new JButton("删除");
		deleteBtn.addActionListener(this);
		deletePanel = new JPanel();
		deletePanel.add(deleteInnerPanel);
		deletePanel.add(inputNoLabel);
		deletePanel.add(inputNoField);
		deletePanel.add(deleteBtn);
		dbTabPane.add("删除数据", deletePanel);

		c.add(BorderLayout.NORTH, dbTabPane);

	}

	public void inputRecords() throws SQLException, ClassNotFoundException { // 录入学生数据
		String id = inputInnerPanel.getId();
		String name = inputInnerPanel.getName();
		String sex = inputInnerPanel.getSex();
		String birthday = inputInnerPanel.getBirth();
		String addres = inputInnerPanel.getAddress();
		String tel = inputInnerPanel.getTel();

		Statement stmt = DButils.getconn();

		String InsSQL;
		InsSQL = "INSERT INTO student(id,name,sex,birthday,addres,tel)"

				+ "VALUES(" + "'" + id + "'," + "'" + name + "'," + "'" + sex + "'," + "'" + birthday + "'," + "'"
				+ addres + "'," + "'" + tel + "')";

		int res = stmt.executeUpdate(InsSQL);
		System.out.println(res);

		JOptionPane.showMessageDialog(null, "插入成功");

	}

	public void viewARecord(String id, StudentPanel p) throws SQLException, ClassNotFoundException {
		try {
			Statement stmt = DButils.getconn();
			String viewSQL = "SELECT * From student where id='" + id + "'";

			ResultSet rs = stmt.executeQuery(viewSQL);

			if (rs.next()) {
				p.setId(rs.getString("id"));
				p.setName(rs.getString("name"));
				p.setSex(rs.getString("sex"));
				p.setBirth(rs.getString("birthday"));
				p.setAddress(rs.getString("addres"));
				p.setTel(rs.getString("tel"));
			}
		} catch (SQLException e) {
			System.out.println("浏览学生记录失败");
			e.printStackTrace();
		} finally {
			DButils.connclose();
		}
	}

	public void viewRecords() throws SQLException, ClassNotFoundException {
		try {
			viewArea.setText("");
			String viewString = "";
			Statement stmt = DButils.getconn();

			ResultSet rs = stmt.executeQuery("SELECT * From student");
			ResultSetMetaData rsMeta = rs.getMetaData();
			int nums = rsMeta.getColumnCount();
			// 获得字段名称
			for (int i = 1; i <= nums; i++) {
				viewString += rsMeta.getColumnName(i) + "\t";
			}
			viewString += "\n";

			// 获得数据表student的记录
			while (rs.next()) {
				for (int i = 1; i <= nums; i++) {
					viewString += rs.getString(i) + "\t";
				}
				viewString += "\n";
				viewArea.setText(viewString);
			}
			rs.close();
		} catch (SQLException e) {
			System.out.println("浏览学生记录失败");
			e.printStackTrace();
		} finally {
			DButils.connclose();
		}
	}

	public void updateRecord(String id) { // 修改学号为no学生的记录

		Statement stmt;
		try {
			stmt = DButils.getconn();

			String sql = "update student set name='" + updateInnerPanel.getName() + "', sex='"
					+ updateInnerPanel.getSex() + "', addres='" + updateInnerPanel.getAddress() + "' where id=" + id;
			int res = stmt.executeUpdate(sql);

			JOptionPane.showMessageDialog(null, "修改数据成功");

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void deleteRecord(String id) throws SQLException, ClassNotFoundException {
		String delStr = "delete from student where id='" + id + "'";
		
		
		
		try {
			Statement stmt = DButils.getconn();
		int res = stmt.executeUpdate(delStr);
		System.out.println(delStr);
			System.out.println(res);
			
			JOptionPane.showMessageDialog(null, "删除成功");
		} catch (SQLException e) {
			System.err.println("删除失败");
			e.printStackTrace();
		} finally {
			DButils.connclose();
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		StudentManagement app = new StudentManagement();
		app.setSize(500, 260);
		app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		app.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getSource() == inputBtn) {
			try {
				try {
					inputRecords();
				} catch (ClassNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

		} else if (e.getSource() == viewBtn) {
			try {
				viewRecords();
			} catch (ClassNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		} else if (e.getSource() == updateBtn) {
			updateRecord(updateInnerPanel.getId());
		} else if (e.getSource() == deleteBtn) {
		
				System.out.println("删除");
				
				System.out.println(deleteInnerPanel.getId());
				
				System.out.println("删除");
				
				try {
					deleteRecord(deleteInnerPanel.getId());
					deleteInnerPanel.clearContent();
					
					
				} catch (ClassNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				
				
	
	
		} else if (e.getSource() == clearBtn1) {
			inputInnerPanel.clearContent();
		}
	}

}